<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <h3>点名器</h3>
    <div id="nameBox"></div>
    <button>开始</button>
    <button>停</button>
    <script>
        var users = ['鲁智深', '柯南', '孙悟空', '奥特曼', '水兵月'];
        var startBtn = document.querySelectorAll('button')[0];
        var endBtn = document.querySelectorAll('button')[1];
        var oDiv = document.querySelector('#nameBox');
        var timer = null;// 必须设置成全局的

        startBtn.onclick = function () {
            timer = setInterval(()=>{
                var index = Math.floor(Math.random() * users.length);// 计算随机索引
                // console.log(users[index]);
                oDiv.innerHTML = users[index];
            },1000/24);
        }
        
        endBtn.onclick = function () {
            clearInterval(timer);
        }
    </script>
</body>

</html>